
import $api from "@/api/index.js"
import $util from "@/utils/util.js"
import $i18n from "@/utils/i18n" // 多语言
export default {
	state: {
		indexConfig: {
			banners: [], // 轮播
			navs: [], // 金刚区
			notices: [], // 公告
			noticesText: [],
		},
		indexNotice:'',
		indexBanner:'',
		indexTabNav:'',
		articleParam: {
			page: 1,
			id: '',
		},
		articleList: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		rewardBanner:'',
		contentText: {
			contentdown: $i18n.tc('app.load.contentdown'),
			contentrefresh:  $i18n.tc('app.load.contentrefresh'),
			contentnomore: $i18n.tc('app.load.contentnomore'),
		}
	},
	mutations: {		
		// 更新状态
		updateContentState(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		
		// 获取首页配置
		async getIndexConfig({
			commit,
			state
		}, param){
			// #ifdef H5
			// 是否存在缓存数据
			const cache = $util.getCache('indexConfig')
			if(cache){
				commit('updateContentState', {
					key: 'indexConfig',
					val: cache
				})
				return
			}
			// #endif
			const res = await $api.content.getIndexInfo()
			if(res.code == 200){
				let old = $util.deepCopy(state.indexConfig)
				let {
					banners,
					notices,
					navs
				} = res.data
				// if(banners.length > 0){
				// 	old.banners = banners
				// }
				if(notices.length > 0){
					old.notices = notices
					let noticesText = []
					for (let i = 0; i < notices.length; i++) {
						noticesText.push(notices[i]['title'])
					}
					old.noticesText = noticesText
				}
				if(navs.length > 0){
					// console.log(res.data.navs)
					let navList = navs.map((item)=>{
						return {
							label: item.title, // 名称
							name: item.image, // 图标
							params: {
								type: item.route.type,
								url: item.route.url,
							},
							show: item.is_display == 1 ? true : false,
						}
					})
					old.navs = navList

				}
				commit('updateContentState', {
					key: 'indexConfig',
					val: old
				})
				$util.setCache('indexConfig', old, 180)
			}
			
		},
		
		async getRewardBanner({
			commit,
			state
		},param){
			var res = await $api.content.getRewardBanner(3)
			
			if(res.code == 200 && res.data.length > 0){
				commit('updateContentState', {
					key: 'rewardBanner',
					val: res.data
				})
			}
		},
		
		// 获取文章列表
		async getArticleList({
			commit,
			state
		}, param) {
			let res = await $api.content.getArticleList(param)
			let oldList = state.articleList;
			let newList = res.data;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data) // 合并数组
				list = newList;
			}
			commit('updateContentState', {
				key: 'articleList',
				val: list
			})
		},
		
	}
}